package pers.vinsonws.jianghu.serverspring.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pers.vinsonws.jianghu.serverspring.channel.ChannelManager;
import pers.vinsonws.jianghu.serverspring.service.ChatRoomServiceImpl;
import pers.vinsonws.jianghu.serverspring.service.UserServiceImpl;
import pers.vinsonws.jianghu.util.ChatRoom;
import pers.vinsonws.jianghu.util.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @auther vinsonws
 * @date 2018/11/6 0:12
 * 主要是登录成功之后的客户端信息的初始化
 * 客户端用户信息的初始化有：
 *                        1、获取全部聊天室信息
 *                        2、获取已加入聊天室信息
 */
@Component
public class InitClientCtrl {
    private static final Logger log = LoggerFactory.getLogger(InitClientCtrl.class);
    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private ChatRoomServiceImpl chatRoomService;

    @Autowired
    private ChannelManager channelManager;

    public List<ChatRoom> getAllChatRoom(){
        return chatRoomService.getAllChatRoom();
    }

    public Set<ChatRoom> getJoinedChatRoom(int userId){
        return chatRoomService.getJoinedChatRoom(userId);
    }

    public List<User> getAllOnlineUsers(){
        List<User> users = new ArrayList<>();
        for (int userId:channelManager.getAllOnlineUserId()){
            users.add(userService.getUserById(userId));
        }
    return users;
    }
}
